package com.jmpt.fangbohui.ui.holder;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.NewHouseDetailBean.HouseImgEntity;
import com.jmpt.fangbohui.ui.ViewPictureActivity;
import com.jmpt.fangbohui.ui.base.BaseViewHolder;
import com.jmpt.fangbohui.utils.ImageLoaderHelper;
import com.jmpt.fangbohui.utils.UIUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * 详情页面 图片
 * Created by Victor on 2015/6/29.
 */
public class HouseDetailImagesHolder extends BaseViewHolder<List<HouseImgEntity>> implements ViewPager.OnPageChangeListener, OnTouchListener {
	
    @ViewInject(R.id.vp_detail_images)
    private ViewPager mVpImages;

    @ViewInject(R.id.tv_detail_image_index)
    private TextView mTvIndex;
    
    private List<HouseImgEntity> mImageDatas;

    public HouseDetailImagesHolder(Context context) {
        super(context);
    }

    @Override
    protected View initView() {
        View view = View.inflate(UIUtils.getContext(), R.layout.detail_head_images, null);

        // View注入
        ViewUtils.inject(this, view);

        return view;
    }

    @Override
    protected void refreshView(List<HouseImgEntity> data) {
    	if(data == null  || data.size() < 1){
    		LogUtils.i("没有图片...");
    		return;
    	}
    	
        // 铺数据
        this.mImageDatas = data;

        // 给viewpager设置数据 Adapter ---> list
        mVpImages.setAdapter(new DetailImagesAdapter());

        // 设置ViewPager的监听
        mVpImages.setOnPageChangeListener(this);

        mVpImages.setCurrentItem(mImageDatas.size() * 1000);

        mVpImages.setOnTouchListener(this);
        
    }
    
    private int startX;
    private int startY;
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()){
            case MotionEvent.ACTION_DOWN:
                startX = (int) (motionEvent.getX() + 0.5f);
                startY = (int) (motionEvent.getY() + 0.5f);
                break;
            case MotionEvent.ACTION_MOVE:
                int dX = (int) (Math.abs(motionEvent.getX() - startX) + 0.5f);
                int dY = (int) (Math.abs(motionEvent.getY() - startY) + 0.5f);
                if(dX > dY){
                    mRootView.getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return false;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        // 选中时切换点
        int count = mImageDatas.size();
        mTvIndex.setText(String.valueOf(position % count + 1) + "/" + count );
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class DetailImagesAdapter extends PagerAdapter
    {

        @Override
        public int getCount()
        {
            // if (mPictureDatas != null) { return mPictureDatas.size(); }
            if (mImageDatas != null && mImageDatas.size() > 0) { return Integer.MAX_VALUE; }
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object)
        {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position)
        {
            ImageView iv = new ImageView(UIUtils.getContext());
            
            iv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					startViewPictureActivity(position % mImageDatas.size());
				}
			});
            
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            // 设置默认图标
//            iv.setImageResource(R.drawable.ic_default);

            // 加载图片
            String uri = mImageDatas.get(position % mImageDatas.size()).getPic();

            ImageLoaderHelper.displayImage(uri, iv);

            container.addView(iv);

            return iv;
        }
       

		@Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            container.removeView((View) object);
        }
    }

    protected void startViewPictureActivity(int position) {
    	if(mImageDatas == null){
    		return;
    	}
		Intent intent = new Intent(UIUtils.getContext(), ViewPictureActivity.class);
		intent.putExtra(ViewPictureActivity.START_INDEX, position);
		String[][] dataArray = getDataArray();
		intent.putExtra(ViewPictureActivity.PICTURE_ARRAY, dataArray[0]);
		intent.putExtra(ViewPictureActivity.TITLE_ARRAY, dataArray[1]);
		mContext.startActivity(intent);
	}
    
    
    private String[][] getDataArray(){
    	if(mImageDatas != null ){
    		String[][] dataArr = new String[2][mImageDatas.size()];
    		String[] imageArr = new String[mImageDatas.size()];
    		String[] titleArr = new String[mImageDatas.size()];
    		for (int i = 0; i < imageArr.length; i++) {
    			imageArr[i] = mImageDatas.get(i).getPic();
    			titleArr[i] = mImageDatas.get(i).getPic_name();
			}
    		dataArr[0] = imageArr;
    		dataArr[1] = titleArr;
    		return dataArr;
    	}
    	return null;
    }
}
